# -*- coding: utf-8 -*-
import logging
import sys
from pylons import request, response, session, tmpl_context as c, url, app_globals as g
from pylons.controllers.util import abort, redirect
from pylons.configuration import asbool
from pylons.decorators.cache import beaker_cache
from pylons import cache

from repoze.what.plugins.pylonshq import ControllerProtector, ActionProtector
from repoze.what.predicates import in_group, is_user, has_permission
from craneserver.lib.queue import CommandQueueController
from craneserver.lib.base import BaseController, render
import simplejson


log = logging.getLogger(__name__)


@ControllerProtector(in_group('Super User'))
class AdminController(CommandQueueController):

#    def available_commands(self, format='json'):
#        uid = c.identity['user'].uid
#        nocache = asbool(request.params.get('nocache', False))
#        @g.cache.region('memcached', uid, str(sys._getframe().f_code.co_name))
#        def available_commands(format):
#            result = g.command_queue.available_commands()
#            if format == 'json':
#                return simplejson.dumps(result)
#            elif format == 'xml':
#                c.title = str(sys._getframe().f_code.co_name)
#                c.content = result
#                return render('index.mako')
#        if nocache:
#            g.cache.region_invalidate(available_commands, None, uid, str(sys._getframe().f_code.co_name), format)
#
#        return available_commands(format)

    def statistics(self, format='json'):

        uid = c.identity['user'].uid
        nocache = asbool(request.params.get('nocache', False))
        @g.cache.region('memcached', uid, str(sys._getframe().f_code.co_name))
        def statistics(format):
            result = g.command_queue.statistics()
            if format == 'json':
                return simplejson.dumps(result)
            elif format == 'xml':
                c.title = str(sys._getframe().f_code.co_name)
                c.content = result
                return render('output.mako')
        if nocache:
            g.cache.region_invalidate(statistics, None, uid, str(sys._getframe().f_code.co_name), format)

        return statistics(format)

    def revoked(self, format='json'):

        uid = c.identity['user'].uid
        nocache = asbool(request.params.get('nocache', False))
        @g.cache.region('memcached', uid, str(sys._getframe().f_code.co_name))
        def revoked(format):
            result = g.command_queue.revoked()
            if format == 'json':
                return simplejson.dumps(result)
            elif format == 'xml':
                c.title = str(sys._getframe().f_code.co_name)
                c.content = result
                return render('output.mako')
        if nocache:
            g.cache.region_invalidate(revoked, None, uid, str(sys._getframe().f_code.co_name), format)

        return revoked(format)


    def scheduled(self, format='json'):

        uid = c.identity['user'].uid
        nocache = asbool(request.params.get('nocache', False))
        @g.cache.region('memcached', uid, str(sys._getframe().f_code.co_name))
        def scheduled(format):
            result = g.command_queue.scheduled()
            if format == 'json':
                return simplejson.dumps(result)
            elif format == 'xml':
                c.title = str(sys._getframe().f_code.co_name)
                c.content = result
                return render('output.mako')
        if nocache:
            g.cache.region_invalidate(scheduled, None, uid, str(sys._getframe().f_code.co_name), format)

        return scheduled(format)


    def active(self, format='json'):

        uid = c.identity['user'].uid
        nocache = asbool(request.params.get('nocache', False))
        @g.cache.region('memcached', uid, str(sys._getframe().f_code.co_name))
        def active(format):
            result = g.command_queue.active()
            if format == 'json':
                return simplejson.dumps(result)
            elif format == 'xml':
                c.title = str(sys._getframe().f_code.co_name)
                c.content = result
                return render('output.mako')
        if nocache:
            g.cache.region_invalidate(active, None, uid, str(sys._getframe().f_code.co_name), format)

        return active(format)

    def enable_events(self, format='json'):
        result = g.command_queue.enable_events()
        if format == 'json':
            return simplejson.dumps(result)
        elif format == 'xml':
            c.title = str(sys._getframe().f_code.co_name)
            c.content = result
            return render('output.mako')

    def disable_events(self, format='json'):
        result = g.command_queue.disable_events()
        if format == 'json':
            return simplejson.dumps(result)
        elif format == 'xml':
            c.title = str(sys._getframe().f_code.co_name)
            c.content = result
            return render('output.mako')

    def discard_all_pending_commands(self, format='json'):
        result = g.command_queue.discard_all_pending_commands()
        if format == 'json':
            return simplejson.dumps(result)
        elif format == 'xml':
            c.title = str(sys._getframe().f_code.co_name)
            c.content = result
            return render('output.mako')

    def ping_workers(self, format='json'):
        result = g.command_queue.ping_workers()
        if format == 'json':
            return simplejson.dumps(result)
        elif format == 'xml':
            c.title = str(sys._getframe().f_code.co_name)
            c.content = result
            return render('output.mako')

    def rate_limit(self, command, rate_limitation, format='json'):
        result = g.command_queue.rate_limit(command, rate_limitation)
        if format == 'json':
            return simplejson.dumps(result)
        elif format == 'xml':
            c.title = str(sys._getframe().f_code.co_name)
            c.content = result
            return render('output.mako')

#    def execute(self, command, format='json'):
#        kwargs = dict(request.params)
#        result = g.command_queue.execute(command, **kwargs)
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def execute_wait(self, command, format='json'):
#        kwargs = dict(request.params)
#        result = g.command_queue.execute_wait(command, **kwargs)
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def cancel(self, commandUuid, format='json'):
#        result = g.command_queue.cancel(commandUuid)
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def status(self, commandUuid, format='json'):
#        result = g.command_queue.status(commandUuid)
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def ready(self, commandUuid, format='json'):
#        result = g.command_queue.ready(commandUuid)
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def successful(self, commandUuid, format='json'):
#        result = g.command_queue.successful(commandUuid)
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def failed(self, commandUuid, format='json'):
#        try:
#            result = g.command_queue.failed(commandUuid)
#        except:
#            result = False
#        if format == 'json':
#            return simplejson.dumps(result)
#        elif format == 'xml':
#            c.title = str(sys._getframe().f_code.co_name)
#            c.content = result
#            return render('index.mako')

#    def traceback(self, commandUuid, format='json'):

#        uid = c.identity['user'].uid
#        nocache = asbool(request.params.get('nocache', False))
#        @g.cache.region('mongodb', uid, str(sys._getframe().f_code.co_name))
#        def traceback(commandUuid, format):
#            result = g.command_queue.traceback(commandUuid)
#            if format == 'json':
#                return simplejson.dumps(result)
#            elif format == 'xml':
#                c.title = str(sys._getframe().f_code.co_name)
#                c.content = result
#                return render('index.mako')
#        if nocache:
#            g.cache.region_invalidate(traceback, None, uid, str(sys._getframe().f_code.co_name), commandUuid, format)


#        return traceback(commandUuid, format)

#    def result(self, commandUuid, format='json'):

#        uid = c.identity['user'].uid
#        nocache = asbool(request.params.get('nocache', False))
#        @g.cache.region('mongodb', uid, str(sys._getframe().f_code.co_name))
#        def result(commandUuid, format):
#            result = g.command_queue.result(commandUuid)
#            if format == 'json':
#                return simplejson.dumps(result)
#            elif format == 'xml':
#                c.title = str(sys._getframe().f_code.co_name)
#                c.content = result
#                return render('index.mako')
#        if nocache:
#            g.cache.region_invalidate(result, None, uid, str(sys._getframe().f_code.co_name), commandUuid, format)

#        return result(commandUuid, format)
